package com.zl.freight.ui.fragment;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zl.freight.R;
import com.zl.freight.mode.AliMsgModel;
import com.zl.freight.utils.API;
import com.zl.freight.utils.DoubleUtils;
import com.zl.freight.utils.SoapCallback;
import com.zl.freight.utils.SoapUtils;
import com.zl.freight.utils.SpUtils;
import com.zl.zlibrary.base.BaseFragment;
import com.zl.zlibrary.utils.GsonUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author zhangleI
 * @date 18/1/14
 * 提现Fragment
 */
public class TiXianFragment extends BaseFragment {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.et_input_money)
    EditText etInputMoney;
    @BindView(R.id.tv_ti_xian_bt)
    TextView tvTiXianBt;
    Unbinder unbinder;
    @BindView(R.id.tv_current_ali_account)
    TextView tvCurrentAliAccount;
    @BindView(R.id.tv_real_money)
    TextView tvRealMoney;
    private String myMoney;
    private double realMoney;

    public TiXianFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_ti_xian, container, false);
        unbinder = ButterKnife.bind(this, view);
        view.setClickable(true);
        initView();
        initData();
        initListener();
        return view;
    }

    private void initListener() {
        etInputMoney.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 2 + 1);
                        etInputMoney.setText(s);
                        etInputMoney.setSelection(s.length());
                    }
                }

                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    etInputMoney.setText(s);
                    etInputMoney.setSelection(2);
                }

                if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        etInputMoney.setText(s.subSequence(0, 1));
                        etInputMoney.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = editable.toString();
                if (!TextUtils.isEmpty(s)) {
                    double i = Double.parseDouble(s);
                    double v = i * 0.006;
                    realMoney = i - v;
                    double data = DoubleUtils.format0_00(realMoney);
                    tvRealMoney.setText("实际到账金额为：" + data);
                } else {

                }
            }
        });
    }

    private void initData() {

    }

    private void initView() {
        tvTitle.setText("提现");
        String bankaccount = SpUtils.getUserData(mActivity).getBankaccount();
        if (!TextUtils.isEmpty(bankaccount)) {
            tvCurrentAliAccount.setText("* " + bankaccount);
        } else {
            tvCurrentAliAccount.setText("* 无");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.iv_back, R.id.tv_ti_xian_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                getFragmentManager().popBackStack();
                break;
            case R.id.tv_ti_xian_bt:
                tiXian();
                break;
        }
    }

    /**
     * 提现的方法,这个方法主要是为了验证
     */
    private void tiXian() {

        hideKeyboard(etInputMoney);

        if (TextUtils.isEmpty(SpUtils.getUserData(mActivity).getBankaccount())) {
            showToast("您还未绑定支付宝账号，请先绑定");
            return;
        }
        final String money = etInputMoney.getText().toString().trim();
        if (TextUtils.isEmpty(money)) {
            showToast("金额不能为空");
            return;
        }

        //用户输入的金额
        final double i;
        /**
         *我已经做了用户输入限制，但是如果有人就是找茬输入了小数点不输入后面小数怎么办
         *这里加一个判断如果小数点后没有内容，我们就将小数点去掉
         */
        if (money.contains(".")) {
            int index = money.lastIndexOf(".");
            if (index == (money.length() - 1)) {
                i = Double.parseDouble(money.replaceAll("\\.", ""));
            } else {
                i = Double.parseDouble(money);
            }
        } else {
            i = Double.parseDouble(money);
        }
        if (i < 100) {
            showToast("金额必须大于100");
            return;
        }

        //手续费v
        double v = i * 0.006;
        //对手续费做四舍五入，保留两位小数
        double s = DoubleUtils.format0_00(v);
        //真正到账的金额
        realMoney = i - s;

        showNotTouchDialog("提现中...");
        Map<String, String> params = new HashMap<>();
        params.put("Id", SpUtils.getUserData(mActivity).getId());
        SoapUtils.Post(mActivity, API.AbleCasch, params, new SoapCallback() {
            @Override
            public void onError(String error) {
                showToast(error);
            }

            @Override
            public void onSuccess(String data) {

                myMoney = data;
                if (TextUtils.isEmpty(myMoney)) {
                    hideDialog();
                    showToast("未获取到账户余额");
                    return;
                }

                try {
                    //用户账户余额
                    double taotal = Double.parseDouble(myMoney);
                    if (taotal >= 100) {
                        if (i > taotal) {
                            hideDialog();
                            showToast("本次最多可以提现" + taotal + "元");
                            return;
                        }

                        commit(i);

                    } else {
                        hideDialog();
                        showToast("账户余额为" + taotal + ",满100元才可提现");
                    }
                } catch (Exception e) {
                    showToast("获取账户余额时出错");
                    return;
                }
            }
        });

    }

    /**
     * 在这里进行真正的提现
     */
    private void commit(final double money) {
        Map<String, String> params = new HashMap<>();
        params.put("UserId", SpUtils.getUserData(mActivity).getId());
        //这个金额是扣除手续费后的,在这里进行保留小数点操作是为了消除计算误差若不消除可能会出现99.4600000000001的情况
        double v = DoubleUtils.format0_00(realMoney);
        params.put("Money", v + "");
        SoapUtils.Post(mActivity, API.DoCasch, params, new SoapCallback() {
            @Override
            public void onError(String error) {
                hideDialog();
                showToast(error);
            }

            @Override
            public void onSuccess(String data) {
                if (!TextUtils.isEmpty(data)) {
                    AliMsgModel aliMsgModel = GsonUtils.fromJson(data, AliMsgModel.class);
                    if (aliMsgModel.getAlipay_fund_trans_toaccount_transfer_response().getCode().equals("10000")) {
                        updateMoney(money);
                    } else if (aliMsgModel.getAlipay_fund_trans_toaccount_transfer_response().getCode().equals("40004")) {
                        hideDialog();
                        showToast("系统维护中暂时无法提现");
                    } else {
                        hideDialog();
                        showToast(aliMsgModel.getAlipay_fund_trans_toaccount_transfer_response().getSub_msg());
                    }
                } else {
                    hideDialog();
                    showToast("系统维护中");
                }
            }
        });
    }

    /**
     * 更新余额
     */
    private void updateMoney(final double money) {
        Map<String, String> params = new HashMap<>();
        params.put("UserId", SpUtils.getUserData(mActivity).getId());
        params.put("Moreorless", "1");
        params.put("PayName", "提现");
        params.put("Value", "" + money);

        SoapUtils.PostNoCheck(mActivity, API.IntegralChange, params, new SoapCallback() {
            @Override
            public void onError(String error) {
                hideDialog();
                showToast(error);
            }

            @Override
            public void onSuccess(String data) {
                hideDialog();
                showToast("提现成功，请注意查收");
                getFragmentManager().popBackStack();
                if (onTiXianListener != null) {
                    onTiXianListener.tiXianSuccess(money);
                }
            }
        });
    }

    private OnTiXianListener onTiXianListener;

    public void setOnTiXianListener(OnTiXianListener onTiXianListener) {
        this.onTiXianListener = onTiXianListener;
    }

    public interface OnTiXianListener {
        void tiXianSuccess(double money);
    }
}
